#include "manager.h"


Node* head = NULL;
int libofbooknum=0;
//创建节点函数
Node* creatNode(Book data){
	Node* pNew = (Node*)malloc(SIZE);
	//异常处理
	if(NULL == pNew) return NULL;
	//初始化
	pNew->data=data;
	pNew->after=pNew->before=NULL;
	return pNew;
}
//删除节点,根据图书名删除或者图书编号
bool delNode(Node ** phead,int bookNum){
	//异常处理
	if(NULL == phead) return false;
	//找到要删除书籍编号的节点
	Node* pdelbooknum=findNode(bookNum);

	//异常处理 看是否找到
	if(pdelbooknum){//找到了
		//如果删除的是头
		if(*phead == pdelbooknum && NULL != (*phead)->after){//删除的是头且链表不止有头
			*phead=pdelbooknum->after;
			pdelbooknum->after->before=NULL;
			//内存释放
			free(pdelbooknum);
			return true;
		}else if(*phead == pdelbooknum && NULL == (*phead)->after){//删除的是头且链表只有头
			//头为空 释放头
			*phead=NULL;
			free(pdelbooknum);
			return true;

		}else if(*phead != pdelbooknum && NULL == pdelbooknum->after){//删除的是尾
			pdelbooknum->before->after=pdelbooknum->after;
			free(pdelbooknum);
			return true;
		}else{//删除的是中间节点
			pdelbooknum->before->after=pdelbooknum->after;
			pdelbooknum->after->before=pdelbooknum->before;
			free(pdelbooknum);
			return true;
		}
		
	}
	return false;//没找到

}
//修改节点
bool changeNode(int bookNum){
	Node* pchange = findNode(bookNum);
	//异常判断
	if(NULL == pchange){//节点不存在 书籍编号输入错误
		printf("书籍编号错误\n");
		return false;
	}else{//找到要修改书籍所在的节点了
		printf("选择要修改的信息\n");
		printf("1:书籍编号\n");
		printf("2:书籍名  \n");
		printf("3:书籍作者\n");
		printf("4:书籍出版社\n");
		printf("5:返回上层界面\n");
		printf("6:退出\n");
		int chnum;
		scanf("%d",&chnum);
		switch(chnum){
			case 1: printf("输入新的书籍编号\n");
					scanf("%d",&pchange->data.bookNum);		 			break;
			case 2: printf("输入新的书籍名\n");
					scanf("%s",pchange->data.bookName);					break;
			case 3: printf("输入新的书籍作者\n");
					scanf("%s",pchange->data.bookAuthor);				break;
			case 4: printf("输入新的书籍出版社\n");
					scanf("%s",pchange->data.bookPublish);				break;
			case 5: managerMenuOfBook();								break;
			case 6: exit(-1);
			default : printf("输入错误，请再次输入\n");changeNode(bookNum);break;
		}
		printf("修改成功\n");
	}
	jindu(2);
	managerMenuOfBook();

}
//节点尾插
void tailNode(Node** phead,Book data){
	//异常处理
	if(NULL == phead) return;
	Node* tail=creatNode(data);
	if(NULL == tail) printf("creatNode创建失败  ");
		if(NULL == *phead){//链表为空
		*phead=tail;
		libofbooknum++;
	}else{//链表不为空，找到尾
		Node* ptemp=*phead;
		while(ptemp->after){
			ptemp=ptemp->after;
		}
		ptemp->after=tail;
		tail->before=ptemp;
		libofbooknum++;
	}

}
//查找节点
Node* findNode(int bookNum){
	Node* ptemp=head;
	while(ptemp){
		if(ptemp->data.bookNum == bookNum) return ptemp;
		ptemp=ptemp->after;
	}
	return NULL;
}
//遍历节点
void travelNode(Node* phead){
	//异常处理
	if(NULL == phead){
		printf("库中未存储书籍信息\n");
		jindu(1);
		managerMenuOfBook();
	} 
	while(phead){
		printf("编号：%2d  书名：%-7s  作者：%-7s  出版社：%-10s\n",
			phead->data.bookNum,phead->data.bookName,
			phead->data.bookAuthor,phead->data.bookPublish);
			phead=phead->after;
	}
	printf("\n");
	printf("库中图书数量为：%d\n",libofbooknum);
	jindu(4);
	managerMenuOfBook();
}
//遍历用户查看专用
void travelNodeofBook(Node* phead){
	//异常处理
	if(NULL == phead){
		printf("库中未存储书籍信息\n");
		jindu(1);
	} 
	while(phead){
		printf("编号：%d  书名：%s  作者：%s  出版社：%s\n",
			phead->data.bookNum,phead->data.bookName,
			phead->data.bookAuthor,phead->data.bookPublish);
			phead=phead->after;
	}
	jindu(4);
}

//管理员插入书籍信息 添加新的书籍
void addBook(){
	system("clear");
	printf("图书管理系统--书籍添加\n");
	Book data;
	printf("请输入书籍编号：\n");
	scanf("%d",&data.bookNum);
	getchar();
	printf("请输入书籍名：\n");
	fgets(data.bookName, sizeof(data.bookName), stdin);  
    data.bookName[strcspn(data.bookName, "\n")] = 0; 
	printf("请输入书籍作者：\n");
	//getchar();
	//scanf("%s",data.bookAuthor);
	fgets(data.bookAuthor, sizeof(data.bookAuthor), stdin);  
    data.bookAuthor[strcspn(data.bookAuthor, "\n")] = 0; 
	printf("请输入书籍出版社：\n");
	//getchar();
	//scanf("%s",data.bookPublish);
	fgets(data.bookPublish, sizeof(data.bookPublish), stdin);  
    data.bookPublish[strcspn(data.bookPublish, "\n")] = 0; 
	tailNode(&head,data);
	printf("添加成功\n");
	saveBook();
	jindu(3);
	managerMenuOfBook();
}
//查找界面
void findBook(){
	system("clear");
	printf("管理员--图书查找\n");
	printf("请输入要找找的图书编号\n");
	int findbooknum;
	scanf("%d",&findbooknum);
	Node* pfindbook=findNode(findbooknum);
	//异常判断
	if(NULL == pfindbook){
		printf("查找失败，请检查编号是否正确\n");
		jindu(1);
		managerMenuOfBook();

	}else{
		printf("查找图书信息为：编号-%d书名-%s作者-%s出版社-%s\n",
			pfindbook->data.bookNum,pfindbook->data.bookName,
			pfindbook->data.bookAuthor,pfindbook->data.bookPublish);
		jindu(3);
		managerMenuOfBook();
	}
}
//删除图书界面
void delBook(){
	system("clear");
	printf("管理员--图书删除\n");
	printf("请输入要删除图书的编号\n");
	int delbooknum;
	scanf("%d",&delbooknum);
	bool delBool = false;
	delBool=delNode(&head,delbooknum);
	if(delBool){//删除成功
		printf("删除成功\n");
		libofbooknum--;
	}else{
		printf("删除失败\n");
	}
	saveBook();
	jindu(3);
	managerMenuOfBook();
}
//修改图书信息
void changeBook(){
	system("clear");
	printf("管理员--修改图书信息\n");
	printf("请输入要修改图书的编号\n");
	int changebooknum;
	scanf("%d",&changebooknum);
	changeNode(changebooknum);
	saveBook();
	jindu(2);
	managerMenuOfBook();
}
//存储图书 存到文件中
void saveBook(){
	FILE* pfile=fopen("booklib.txt","w");
	//异常处理
	if(NULL == pfile){
		printf("文件打开失败\n");
		return ;
	}
	Node* pTemp=head;
	while(pTemp){
		fwrite(&(pTemp->data),1,sizeof(Book),pfile);
		pTemp=pTemp->after;
	}
	fclose(pfile);
}
//从文件中读取数据 组成新的链表
void readBook(Node** phead){
	FILE* fpread=fopen("booklib.txt","r");
	if(NULL == fpread){
		printf("文件打开失败\n");
		return ;
	}
	int r;
	Book temp;
	while(1){
		r = fread(&temp,1,sizeof(Book),fpread);
		if(r <= 0) break;
		tailNode(phead,temp);
	}
	fclose(fpread);
}
//查找节点 通过书籍名
Node* findNodebyName(char* bookname){
	//printf("到这里了\n");
	Node* ptemp=head;
	while(ptemp){
		if(!strcmp(ptemp->data.bookName,bookname))  return ptemp;
		ptemp=ptemp->after;
	}
	printf("没有书\n");
	return NULL;
}
//查找节点 通过书籍作者
Node* findNodebyAuthor(char* bookauthor){
	Node* ptemp=head;
	while(ptemp){
		if(!strcmp(ptemp->data.bookAuthor,bookauthor))  return ptemp;
		ptemp=ptemp->after;
	}
	return NULL;


}